package com.ysy.logistics.service.supplier.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.logistics.entities.supplier.SupGoods;
import com.ysy.logistics.entities.supplier.SupOrderGoods;
import com.ysy.logistics.mapper.supplier.SupGoodsMapper;
import com.ysy.logistics.mapper.supplier.SupOrderGoodsMapper;
import com.ysy.logistics.service.supplier.SupOrderGoodsService;
import com.ysy.logistics.service.supplier.SupOrderService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/25 14:59
 **/
@Service
public class SupOrderGoodsServiceImpl extends ServiceImpl<SupOrderGoodsMapper, SupOrderGoods> implements SupOrderGoodsService {

    @Resource
    private SupOrderGoodsMapper supOrderGoodsMapper;
    @Resource
    private SupGoodsMapper supGoodsMapper;
    @Override
    public List<SupGoods> listByOrderId(String id) {
        if (StringUtils.isBlank(id)){
            return null;
        }
        //查询出商品Id
        LambdaQueryWrapper<SupOrderGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SupOrderGoods::getGoodsId).eq(SupOrderGoods::getOrderId,id);
        List<SupOrderGoods> supOrderGoods = supOrderGoodsMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(supOrderGoods)){
            return null;
        }
        //根据商品Id查询商品信息
        LambdaQueryWrapper<SupGoods> supGoodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        supGoodsLambdaQueryWrapper.in(SupGoods::getId, supOrderGoods.stream().map(SupOrderGoods::getGoodsId).toList());
        return supGoodsMapper.selectList(supGoodsLambdaQueryWrapper);
    }

    @Override
    public Long totalGoods(String orderId, String goodsId) {
        LambdaQueryWrapper<SupOrderGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SupOrderGoods::getGoodsNum)
        .eq(SupOrderGoods::getOrderId,orderId).eq(SupOrderGoods::getGoodsId,goodsId);
        List<SupOrderGoods> supOrderGoods = supOrderGoodsMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(supOrderGoods)){
            return 0L;
        }
        return supOrderGoods.stream().mapToLong(SupOrderGoods::getGoodsNum).sum();
    }
}
